package com.ruoyi.project.cms.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.domain.Article;
import com.ruoyi.project.cms.service.ArticleService;

@Controller
@RequestMapping("/site/article")
public class ArticleController extends BaseController {
	private String prefix = "cms/article";

	@Autowired
	private ArticleService articleService;

	@RequiresPermissions("cms:article:view")
	@GetMapping()
	public String article() {
		return prefix + "/article";
	}

	/**
	 * 查询文章列表
	 */
	@RequiresPermissions("cms:article:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Article article) {
		startPage();
		List<Article> list = articleService.selectArticleList(article);
		return getDataTable(list);
	}

	/**
	 * 导出文章列表
	 */
	@RequiresPermissions("cms:article:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Article article) {
		List<Article> list = articleService.selectArticleList(article);
		ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
		return util.exportExcel(list, "article");
	}

	/**
	 * 新增文章
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存文章
	 */
	@RequiresPermissions("cms:article:add")
	@Log(title = "文章", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Article article) {
		return toAjax(articleService.save(article));
	}

	/**
	 * 修改文章
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		Article article = articleService.selectArticle(id);
		mmap.put("article", article);
		return prefix + "/edit";
	}

	/**
	 * 修改保存文章
	 */
	@RequiresPermissions("cms:article:edit")
	@Log(title = "文章", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Article article) {
		return toAjax(articleService.updateArticle(article));
	}

	/**
	 * 删除文章
	 */
	@RequiresPermissions("cms:article:remove")
	@Log(title = "文章", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(articleService.deleteById(ids));
	}
}
